/**
 * KopsoxFramework
 * Copyright (C) 2010 Free Software Foundation, Inc. <http://fsf.org/>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.kopsox.util.xml.parser.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import org.kopsox.general.priority.PriorityEnum;
import org.kopsox.util.xml.data.XMLContainerDescriptor;
import org.kopsox.util.xml.data.impl.NameNamespaceKey;
import org.kopsox.util.xml.data.impl.NamePriorityDescriptor;
import org.kopsox.util.xml.data.impl.XMLContainerDescriptorImpl;
import org.kopsox.util.xml.parser.XMLEnum;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

/**
 * @author Konrad Renner - 23.07.2010
 *
 */
public class ContainerXMLParser extends AbstractKopsoxXMLParser {
	
	private int currentIndex;
	private final List<XMLContainerDescriptor> descriptors;
	
	public ContainerXMLParser(String file) {
		super(file);
		this.descriptors = new ArrayList<XMLContainerDescriptor>();
		this.currentIndex = -1;
	}
	
	public List<XMLContainerDescriptor> getContainerDescriptors() {
		return this.descriptors;
	}

	/* (non-Javadoc)
	 * @see org.kopsox.util.xml.parser.impl.AbstractKopsoxXMLParser#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
	 */
	@Override
	public void startElement(String namespaceURI, String localName,String qName, Attributes atts) throws SAXException {
		
		if(XMLEnum.CONTAINER.toString().equals(qName)) {
	    	String name = atts.getValue(0);
	    	XMLContainerDescriptorImpl impl = new XMLContainerDescriptorImpl();
	    	impl.setName(name);
	    	impl.setNamespace(atts.getValue(XMLEnum.NAMESPACE.toString()));
	    	
	    	this.descriptors.add(impl);
	    	this.currentIndex++;
	    	impl.setKeyNames(new HashSet<String>());
	    }else if(XMLEnum.TYPE.toString().equals(qName)) {
	    	XMLContainerDescriptorImpl impl = (XMLContainerDescriptorImpl)this.descriptors.get(this.currentIndex);
	    	
	    	impl.setClassName(atts.getValue(0));
	    }else if(XMLEnum.ISAUTOVALIDATING.toString().equals(qName)) {
	    	XMLContainerDescriptorImpl impl = (XMLContainerDescriptorImpl)this.descriptors.get(this.currentIndex);
	    	
	    	impl.setAutoValidationEnabled(Boolean.parseBoolean(atts.getValue(0)));
	    }else if(XMLEnum.ISAUTOERRORDELEGATION.toString().equals(qName)) {
	    	XMLContainerDescriptorImpl impl = (XMLContainerDescriptorImpl)this.descriptors.get(this.currentIndex);
	    	
	    	impl.setAutoErrorDelegationEnabled(Boolean.parseBoolean(atts.getValue(0)));
	    }else if(XMLEnum.VALUE.toString().equals(qName)) {
	    	XMLContainerDescriptor valdesc = this.descriptors.get(this.currentIndex);
	    	
	    	NameNamespaceKey key;
	    	key = new NameNamespaceKey(atts.getValue(0),atts.getValue(XMLEnum.NAMESPACE.toString()));
	    	valdesc.getValues().add(key);
	    	
	    	String isKey = atts.getValue(XMLEnum.ISKEYELEMENT.toString());
	    	if(isKey != null && isKey.equalsIgnoreCase("true")) {
	    		valdesc.getKeyNames().add(key.getName());
	    	}
	    }else if(XMLEnum.VALUES.toString().equals(qName)) {
	    	XMLContainerDescriptorImpl impl = (XMLContainerDescriptorImpl)this.descriptors.get(this.currentIndex);
	    	
	    	impl.setValues(new HashSet<NameNamespaceKey>());
	    }else if(XMLEnum.OBSERVERS.toString().equals(qName)) {
	    	XMLContainerDescriptorImpl impl = (XMLContainerDescriptorImpl)this.descriptors.get(this.currentIndex);
	    	
	    	impl.setObservers(new HashSet<NamePriorityDescriptor>());
	    }else if(XMLEnum.VALIDATORS.toString().equals(qName)) {
	    	XMLContainerDescriptorImpl impl = (XMLContainerDescriptorImpl)this.descriptors.get(this.currentIndex);
	    	
	    	impl.setValidators(new HashSet<NamePriorityDescriptor>());
	    }else if(XMLEnum.OBSERVER.toString().equals(qName)) {
	    	XMLContainerDescriptorImpl impl = (XMLContainerDescriptorImpl)this.descriptors.get(this.currentIndex);
	    	
	    	String priority;
	    	if(atts.getLength() > 1) {
	    		priority = atts.getValue(1);
	    	}else {
	    		priority = Integer.toString(PriorityEnum.MEDIUM.getPriority().getPriorityValue());
	    	}
	    	
	    	
	    	NamePriorityDescriptor desc = new NamePriorityDescriptor(atts.getValue(0),priority);
	    	
	    	impl.getObservers().add(desc);
	    }else if(XMLEnum.VALIDATOR.toString().equals(qName)) {
	    	XMLContainerDescriptorImpl impl = (XMLContainerDescriptorImpl)this.descriptors.get(this.currentIndex);
	    	
	    	String priority;
	    	if(atts.getLength() > 1) {
	    		priority = atts.getValue(1);
	    	}else {
	    		priority = Integer.toString(PriorityEnum.MEDIUM.getPriority().getPriorityValue());
	    	}
	    	
	    	
	    	NamePriorityDescriptor desc = new NamePriorityDescriptor(atts.getValue(0),priority);
	    	
	    	impl.getValidators().add(desc);
	    }
		
	}

	/* (non-Javadoc)
	 * @see org.kopsox.util.xml.parser.impl.AbstractKopsoxXMLParser#getPathToDTD()
	 */
	@Override
	public String getPathToDTD() {
		return "org/kopsox/environment/proxy/config/xml/containersXMLDefinition.dtd";
	}
}
